package com.quectel.aiot.service.impl;

import com.quectel.aiot.entity.Role;
import com.quectel.aiot.mapper.RoleMapper;
import com.quectel.aiot.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Role getRoleById(Long id) {
        return roleMapper.selectRoleById(id);
    }

    @Override
    public List<Role> getRoleList(String name, String appId, int page, int size) {
        int offset = (page - 1) * size;
        return roleMapper.selectRoleList(name, appId, offset, size);
    }

    @Override
    public int getRoleCount(String name, String appId) {
        return roleMapper.countRoleList(name, appId);
    }

    @Override
    public void addRole(Role role) {
        // 无条件生成雪花ID，覆盖前端传来的 id
        com.quectel.aiot.util.SnowflakeIdWorker idWorker = new com.quectel.aiot.util.SnowflakeIdWorker(1, 1);
        role.setId(idWorker.nextId());
        role.setCreatedTime(new java.util.Date());
        roleMapper.insertRole(role);
    }

    @Override
    public int updateRole(Role role) {
        return roleMapper.updateRole(role);
    }

    @Override
    public void deleteRole(Long id) {
        roleMapper.deleteRole(id);
    }
} 